#include<stdio.h>
#include<stdlib.h>

struct node
{
	int data;
	struct node *next;
}*head;

typedef struct node NODE;

void append(int value)
{
	NODE *temp, *right;
	temp = (NODE *)malloc(sizeof(NODE));
	temp->data = value;
	right = (NODE *)head;
	while(right->next != NULL) right = right->next;
	right->next = temp;
	right = temp;
	right->next = NULL;
}

void add( int num )
{
    NODE *temp;
    temp=(NODE *)malloc(sizeof(NODE));
    temp->data=num;
    if (head== NULL)
    {
	    head=temp;
	    head->next=NULL;
    }
    else
    {
	    temp->next=head;
	    head=temp;
    }
}
void addafter(int num, int loc)
{
	int i;
	NODE *temp,*left,*right;
	right=head;
	for(i=1;i<loc;i++)
	{
		left=right;
		right=right->next;
	}
	temp=(NODE *)malloc(sizeof(NODE));
	temp->data=num;
	left->next=temp;
	left=temp;
	left->next=right;
	return;
}


void insert(int value)
{
	int c = 0;
	NODE *temp;
	temp = head;
	if(temp == NULL) add(value);
	else
	{
		c++;
		/*while(temp != NULL)
		{
			if(temp->data < value) c++;
			temp = temp->next;
		}*/
		if(c == 0) add(value);
		//else if(c < count()) addafter(value, ++c);
		else append(value);
		//c++;
	}
}

int count()
{
	NODE *n;
	int c = 0;
	n = head;
	while(n != NULL)
	{
	n = n->next;
	c++;
	}
	return c;
}

void print(struct node *read)
{
	read = head;
	if(read == NULL)
	{
		return;
	}
	while(read != NULL)
	{
		printf("%d ",read->data);
		read = read->next;
	}
	printf("\n");
}

int print(struct node *read)
{
	read = head;
	if(read == NULL)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

void main()
{
	NODE *adc;
	head = NULL;

	insert(0);
	insert(10);
	insert(3);
	insert(4);
	insert(8);
	insert(3);


	count();
	printf("displaying:\n");
	print(adc);
}
